﻿CREATE PROCEDURE [dbo].[spCleanupOrphans]
AS
	-- Clean up all but the most recent 10 orphaned RMAReturn records.
	-- These records get created when a new RMA is started, but not completed.
	delete from tblRMAReturn
	where IssueDate is null
		and RMAReturnID not in ( 
			select top 10 RMAReturnID from tblRMAReturn where IssueDate is null order by RMAReturnID desc )

	delete from tblRefund
	where Cost = 0
		and RefundID not in (
			select top 50 RefundID from tblRefund where Cost = 0 order by RefundID desc )
RETURN 0